package net.xumf.domain.master;

import lombok.Data;
import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

@Data
@Entity
@DynamicInsert
@Table(name = "dict", uniqueConstraints = {@UniqueConstraint(columnNames = {"parent_id", "name"})})
public class Dict implements Serializable {

    public Dict(String name, String value) {
        this.name = name;
        this.value = value;
    }

    public Dict(String value) {
        this.value = value;
    }

    public Dict() {
    }

    public Dict(Integer parentId, String key, String name, String value, Boolean hasChild, Integer sequence, String comment, Date createdAt, Date updatedAt) {
        this.parentId = parentId;
        this.key = key;
        this.name = name;
        this.value = value;
        this.hasChild = hasChild;
        this.sequence = sequence;
        this.comment = comment;
        this.createdAt = createdAt;
        this.updatedAt = updatedAt;
    }

    // 字典id
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "dict_id")
    private Integer dictId;

    // 父id（默认为0）
    @Column(name = "parent_id", nullable = false)
    @ColumnDefault("0")
    private Integer parentId;

    // 获取值得key
    @Column(name = "key", nullable = false, unique = true)
    private String key;

    // 名称
    @Column(name = "name", length = 50, nullable = false)
    private String name;

    // 值
    @Column(name = "value", nullable = false)
    @ColumnDefault("")
    private String value;

    // 否存在子模块[0:否,1:是]
    @Column(name = "has_child", nullable = false)
    @ColumnDefault("false")
    private Boolean hasChild;

    // 系统模块在当前父模块下的排序顺序
    @Column(name = "sequence", nullable = false)
    @ColumnDefault("1")
    private Integer sequence;

    // 系统模块备注
    @Column(name = "comment", length = 50, nullable = false)
    private String comment;

    // 创建时间
    @Column(name = "created_at", nullable = false, insertable = false, updatable = false, columnDefinition = "timestamp default current_timestamp")
    @Temporal(TemporalType.DATE)
    @Generated(GenerationTime.INSERT)
    private Date createdAt;

    // 更新时间
    @Column(name = "updated_at", nullable = false, insertable = false, columnDefinition = "timestamp default current_timestamp")
    @Temporal(TemporalType.DATE)
    @Generated(GenerationTime.ALWAYS)
    private Date updatedAt;

}
